

/****************************************************
// 功能：小兵逻辑层
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/2 15:43:30
*****************************************************/

using PEMath;

public class Soldier : MainLogicUnit
{
    public int soldierId;
    public int waveIndex;
    public int orderIndex;
    public string soldierName;

    private PEInt sqrSearchDis;
    private TargetCfg cfg;
    private readonly int AITickInterval = 5;
    private int AITickIntervalCounter = 0;
    
    public Soldier(SoldierData unitData) : base(unitData)
    {
        soldierId = unitData.soldierId;
        waveIndex = unitData.waveIndex;
        orderIndex = unitData.orderIndex;

        unitType = UnitTypeEnum.Soldier;
        unitName = base.unitData.unitConfig.unitName + " w:" + waveIndex + " o:" + orderIndex;

        pathPrefix = "Chars";
    }
    
    public override void LogicInit() {
        base.LogicInit();
        sqrSearchDis = (PEInt)skillArr[0].config.targetCfg.searchDis * (PEInt)skillArr[0].config.targetCfg.searchDis;
        cfg = skillArr[0].config.targetCfg;
        InputMoveForwardKey();
    }

    
    public override void LogicTick() {
        base.LogicTick();
        if(AITickIntervalCounter < AITickInterval) {
            AITickIntervalCounter += 1;
            return;
        }
        else {
            AITickIntervalCounter = 0;
        }

        if(CancelReleaseSkill(unitData.unitConfig.skillArr[0])) {
            MainLogicUnit lockTarget = CalcRule.FindSingleTargetByRule(this, cfg, PEVector3.zero);
            if(lockTarget != null) {
                skillArr[0].ReleaseSkill(PEVector3.zero);
            }
            else {
                lockTarget = CalcRule.FindMinDisEnemyTarget(this, cfg);
                if(lockTarget != null) {
                    PEVector3 offsetDir = lockTarget.LogicPos - LogicPos;
                    PEInt sqrDis = offsetDir.sqrMagnitude;
                    if(sqrDis < sqrSearchDis) {
                        InputFakeMoveKey(offsetDir.normalized);
                    }
                    else {
                        InputMoveForwardKey();
                    }
                }
            }
        }
    }

    private void InputMoveForwardKey() {
        InputFakeMoveKey(IsTeam(TeamEnum.Blue) ? PEVector3.right : PEVector3.left);
    }
}
